package freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  通过模板将对象转换成json
 * Created by Administrator on 2016/12/21.
 *
 * @author zlf
 */
public class JsonUtil {
    @Test
    public void test(){
        Configuration config=new Configuration(Configuration.VERSION_2_3_0);

        try {
            config.setDirectoryForTemplateLoading(new File("/home/zlf/learn/IdeaProjects/web/web-template-test/test/module-freemarker/src/main/resources"));
//            TemplateLoader loader=null;
//            config.setTemplateLoader(loader);
            Map map=new HashMap<>();
            Student student=new Student();
            student.setAge(12);
            student.setDate(new Date());
            student.setName("sssdasd");
            String[] strs={"sas","sas"};
            student.setArr(strs);
            byte b=1;
            student.setB(b);
            student.setBoo(true);
            byte[] bs={1,2,1};
            student.setBs(bs);
            map.put("object", student);
            Template template=config.getTemplate("json.ftl");
            template.process(map, new OutputStreamWriter(System.out));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }


    }

}
